package chapt04.atom;
// Atom.java
// OpenGL SuperBible
// Demonstrates OpenGL coordinate transformation
// Program by Richard S. Wright Jr.

import javax.media.opengl.GL2;
import com.jogamp.opengl.util.gl2.GLUT;

public class Atom
{
	// Rotation amounts
	static float xRot = 0.0f;
	static float yRot = 0.0f;
	
	// Angle of revolution around the nucleus
	static float fElect1 = 0.0f;
	
	// This function does any needed initialization on the context.
	protected static void setup(GL2 gl2, int width, int height)
	{
		gl2.glEnable(GL2.GL_DEPTH_TEST); // Hidden surface removal
		gl2.glFrontFace(GL2.GL_CCW);     // Counter clock-wise polygons face out
		gl2.glEnable(GL2.GL_CULL_FACE);  // Do not calculate inside of jet
	
		// Black background
		gl2.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	}
	
	// Called to draw scene
	protected static void render(GL2 gl2, int w, int h)
	{
		float nRange = 100.0f;
		
		// Prevent a divide by zero
		if(h == 0)
			h = 1;
		
		// Set Viewport to window dimensions
		gl2.glViewport(0, 0, w, h);
		
		// Reset coordinate system
		gl2.glMatrixMode(GL2.GL_PROJECTION);
		gl2.glLoadIdentity();
		
		// Establish clipping volume (left, right, bottom, top, near, far)
		if(w <= h)
			gl2.glOrtho(-nRange, nRange, -nRange * h / w, nRange * h / w, -nRange * 2.0f, nRange * 2.0f);
		else
			gl2.glOrtho(-nRange * w / h, nRange * w / h, -nRange, nRange, -nRange * 2.0f, nRange * 2.0f);
		
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		GLUT glut = new GLUT();
		
		// Clear the window with current clearing color
		gl2.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT);
		
		// Reset the modelview matrix
		gl2.glMatrixMode(GL2.GL_MODELVIEW);
		gl2.glLoadIdentity();
		
		// Translate the whole scene out and into view
		// This is the initial viewing transformation
		gl2.glTranslatef(0.0f, 0.0f, -100.0f);
		
		// Red Nucleus
		gl2.glColor3ub((byte)255, (byte)0, (byte)0);
		glut.glutSolidSphere(10.0f, 15, 15);
		
		// Yellow Electrons
		gl2.glColor3ub((byte)255, (byte)255, (byte)0);
		
		// First Electron Orbit
		// Save viewing transformation
		gl2.glPushMatrix();
		
		// Rotate by angle of revolution
		gl2.glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
		
		// Translate out from origin to orbit distance
		gl2.glTranslatef(90.0f, 0.0f, 0.0f);
		
		// Draw the electron
		glut.glutSolidSphere(6.0f, 15, 15);
		
		// Restore the viewing transformation
		gl2.glPopMatrix();
		
		// Second Electron Orbit
		gl2.glPushMatrix();
		gl2.glRotatef(45.0f, 0.0f, 0.0f, 1.0f);
		gl2.glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
		gl2.glTranslatef(-70.0f, 0.0f, 0.0f);
		glut.glutSolidSphere(6.0f, 15, 15);
		gl2.glPopMatrix();
		
		// Third Electron Orbit
		gl2.glPushMatrix();
		gl2.glRotatef(360.0f - 45.0f, 0.0f, 0.0f, 1.0f);
		gl2.glRotatef(fElect1, 0.0f, 1.0f, 0.0f);
		gl2.glTranslatef(0.0f, 0.0f, 60.0f);
		glut.glutSolidSphere(6.0f, 15, 15);
		gl2.glPopMatrix();
		
		// Increment the angle of revolution
		fElect1 += 10.0f;
		if(fElect1 > 360.0f)
			fElect1 -= 360.0f;
		
		// Show the image
		gl2.glFlush();
	}
}